package chpt09;

class Task implements Runnable {
	private int count;
	public Task(int count) {
		this.count = count;
	}
	@Override
	public void run() {
		String name = Thread.currentThread().getName();
		for (int i = 1; i <= count; i++) {
			System.out.println("线程" + name + "完成任务" + i + "次");
		}
	}
}

public class FirstRunnableTest {
	public static void main(String[] args) {
		Runnable task1 = new Task(3);
		Runnable task2 = new Task(2);
		Thread t1 = new Thread(task1); // 将任务1放入线程t1
		Thread t2 = new Thread(task2); // 将任务2放入线程t2
		t1.start(); // start()方法启动线程
		t2.start();
		// testAnnonymonus();
		// testLambda();
	}

	// testAnnonymonus
	public static void testAnnonymonus() {
		int count = 5;
		new Thread(new Runnable() {
			@Override
			public void run() {
				String name = Thread.currentThread().getName();
				for (int i = 1; i <= count; i++) {
					System.out.println("线程" + name + "完成任务" + i + "次");
				}

			}
		}).start();
	}

	// testLambda
	public static void testLambda() {
		int count = 6;
		new Thread(() -> {
			String name = Thread.currentThread().getName();
			for (int i = 1; i <= count; i++) {
				System.out.println("线程" + name + "完成任务" + i + "次");
			}
		}).start();
	}
}
